package com.csc.util;

import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.csc.models.User;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;

public class DBUtils {
	String url = "jdbc:mysql://localhost:3306/huynhminhtri";
	String username = "root";
	String password = "root";
	Connection connection = null;
	Statement statement = null;

	public boolean initialize() {

		try {

			Class.forName("com.mysql.jdbc.Driver");
			connection = (Connection) DriverManager.getConnection(url,
					username, password);
			createStatement();

		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		if (connection == null) {
			throw new NullPointerException("Null pointer");
		}

		return true;
	}

	public void createStatement() {
		if (statement == null) {
			try {
				statement = (Statement) connection.createStatement();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public User checkUsernamePassword(String username, String password) {
		String query = "SELECT * FROM users WHERE users.userName='" + username
				+ "' and users.password='" + password + "'";
		ResultSet resultSet = null;
		try {
			resultSet = statement.executeQuery(query);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		try {
			if (resultSet.next()) {
				String firstName = resultSet.getString("firstName");
				String lastName = resultSet.getString("lastName");
				String userName = resultSet.getString("userName");
				String passWord = resultSet.getString("passWord");
				String email = resultSet.getString("email");
				String phone = resultSet.getString("phone");
				
//				String stt = firstName + " " + lastName + " " + userName + " " + password + " " + email + " " + phone;
//				System.out.println(stt);
				
				User user = new User(firstName, lastName, userName, email, passWord, phone);
				return user;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				connection.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return null;
	}

	public boolean addUser(String firstname, String lastname, String username,
			String email, String password, String phone) {
		String query = "INSERT INTO users(firstName, lastName, username, password, email, phone) VALUE (?, ?, ?, ?, ?, ?)";
		try {
			PreparedStatement preparedStmt = connection.prepareStatement(query);
			preparedStmt.setString(1, firstname);
			preparedStmt.setString(2, lastname);
			preparedStmt.setString(3, username);
			preparedStmt.setString(4, password);
			preparedStmt.setString(5, email);
			preparedStmt.setString(6, phone);

			preparedStmt.execute();

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		} finally {
			try {
				connection.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return true;
	}

	public boolean updateProfile(String firstname, String lastname,
			String username, String email, String password, String phone) {
		String query = "UPDATE users" + 
			" SET firstName=?, lastName=?, password=?, email=?, phone=?" +
				" WHERE username=?";
		try {
			PreparedStatement preparedStmt = connection.prepareStatement(query);
			preparedStmt.setString(1, firstname);
			preparedStmt.setString(2, lastname);
			preparedStmt.setString(3, password);
			preparedStmt.setString(4, email);
			preparedStmt.setString(5, phone);
			preparedStmt.setString(6, username);

			preparedStmt.execute();

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		} finally {
			try {
				connection.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return true;
	}

	public static void main(String[] args) {
		DBUtils db = new DBUtils();
		db.initialize();

//		if (db.checkUsernamePassword("hmtri", "12345")) {
//			System.out.println("success");
//		} else {
//			System.out.println("fail");
//		}
	}
}
